package com.demo.Controller;

import java.util.List;

import javax.ws.rs.DefaultValue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.entity.Course;
import com.demo.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/Course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	
	@RequestMapping(value="/addCourse")
	public String addCourse(Course course) throws Exception{
		int addCourse = courseService.addCourse(course);
		if(addCourse>0)
			return "ok";
		else 
			return "error";
	}
	
	@RequestMapping(value="/deleteCourse/{id}")
	public String deleteCourse(@PathVariable String id)throws Exception{
		int deleteCourseById = courseService.deleteCourseById(id);
		if(deleteCourseById>0)
			return "ok";
		else 
			return "error";
	}
	
	@RequestMapping(value="/updateCourseById")
	public String updateCourseById( Course course)throws Exception{
		int updateCourser = courseService.updateCourser(course);
		if(updateCourser>0)
			return "ok";
		else 
			return "error";
	}
	
	@RequestMapping(value="/findCourseById/{id}")
	public Course findCourseById(@PathVariable String id)throws Exception{
		Course selectCourseById = courseService.selectCourseById(id);
		return selectCourseById;
	}
	
	@RequestMapping(value="/findAllCourse")
	public PageInfo<Course> findAllCourse(Course course, @DefaultValue("1") Integer pageNum ,  @DefaultValue("5") Integer pageSize) throws Exception{
		PageHelper.startPage(pageNum, pageSize);
		course.setId(course.getId().trim());
		course.setCoursename(course.getCoursename().trim());
		List<Course> selectAllCourse = courseService.selectAllCourse(course);
		PageInfo<Course> pageInfo = new PageInfo<Course>(selectAllCourse);
		return pageInfo;
	}
	@RequestMapping(value="/findAllCourseDistinct")
	public List<Course> findAllCourseDistinct() throws Exception{
		return courseService.selectAllCourseDistinct();
	}
}
